#!/usr/bin/env python
# -*- coding: utf-8 -*-

import json
import time
import os
import re
import json
try:
    import requests
except:
    os.system('pip install requests')

f = open("/opt/app/open-falcon/agent/config/cfg.json")
agentInfo = json.load(f).get('hostname')
check_endpoint='http://'+ agentInfo + '/api/systeminfo'

def get_check():
    try:
        response = requests.get(check_endpoint,timeout=1)
        if response.status_code == 200:
            response = response.content
            response = json.loads(response)
            if response.get('registry_url'):
                status = 0
            else:
                status = 1
        else:
            status = 1
    except:
        status = 1
    return status


def create_record(value):
    record = {}
    record['Metric']      = "harbor.alive"
    record['Endpoint']    = agentInfo
    record['Timestamp']   = int(time.time())
    record['Step']        = 30
    record['Value']       = value
    record['CounterType'] = 'GAUGE'
    record['TAGS']        = ''
    return record

if __name__ == '__main__':
    data=[]
    value = get_check()
    data.append(create_record(value))
    print json.dumps(data)
